import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {path: '/',redirect: '/login'},
    {
      path: '/login',
      name: 'login',
      component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue')
    },
    {
        path: '/home',
        name: 'home',
        component: () => import(/* webpackChunkName: "Home" */ '../views/Home.vue'),
        redirect: '/welcome',
        children:[
            {
                path: '/welcome',
                name: 'welcome',
                component: () => import(/* webpackChunkName: "Welcome" */ '../views/Welcome.vue')
            },
            {
                path: '/users',
                name: 'users',
                component: () => import(/* webpackChunkName: "users" */ '../views/users/Users.vue')
            },
            {
                path: '/rights',
                name: 'rights',
                component: () => import(/* webpackChunkName: "rights" */ '../views/rights/Administration.vue')
            },
            {
                path: '/roles',
                name: 'roles',
                component: () => import(/* webpackChunkName: "roles" */ '../views/rights/Roles.vue')
            },
            {
                path: '/categories',
                name: 'categories',
                component: () => import(/* webpackChunkName: "categories" */ '../views/goods/Categories.vue')
            }

        ]
    }
]
  
const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    let isLogin = localStorage.getItem("token");
    if(to.path == "/logout"){
        localStorage.removeItem('token');
    }else if(to.path == "/login"){
        if(isLogin != null){
            next("/home")
        }
    }else if(isLogin == null){
        next("/login")
    }
    next()
})

export default router
